<?php
namespace app\controllers\crontab;

use Yii;
use app\models\marketing\User;
use app\models\marketing\UserTag;

/**
 * 把第三方分群用户刷入到user表中
 */
class DealTagUserController extends \app\base\Controller 
{
    public function init()
    {
        yii::$app->db->enableSlaves = false;        // 禁用从库查询，从主库查询数据。
    }
    /**
     * yumao
     * 2015-5-22
     * 把第三方分群的相关数据加入到user表中
     */
    public function actionDealTagUser()
    {
        ignore_user_abort(true); 
        set_time_limit(0);
        // 从user_ebay_tag表中取出指定数量的数据
        $oneCount = 20000;
        $userTag = new UserTag(31);
        $user = new User(31);
        $userInfoArr = $userTag->getUserUnDeal($oneCount); // 查出指定数量数据
        $userInfoArrDeal = [];
        $emailArr = [];
        $userInsert = []; // 需要插入的数据
        $userUpdate = []; // 需要更新的用户数据
        $idArr =[];
        // 处理数据去重 
        $oneTimeDeal = 1000; // 一次处理1000条数据
        $i=0;
        foreach ($userInfoArr as $userInfoArrOne) {
            $idArr[] = $userInfoArrOne['id'];
            if (!in_array($userInfoArrOne['email'],$emailArr)) {
                $userInfoArrDeal[$userInfoArrOne['email']] = $userInfoArrOne;
                $emailArr[] = $userInfoArrOne['email'];
                $i++;
                if ($i >= $oneTimeDeal) { // 集满指定的数据
                    try {
                        // 查询处理数据
                        $trans = Yii::$app->db->beginTransaction(); // 开启事务
                        // 从user表中查询数据
                        $emailArrUserTab = $user->getUserInfoByEmail(31,$emailArr);
                        if ($emailArrUserTab) {
                            $emailArrUserTabDeal = [];
                            $emailArrUserTabDealArr = [];
                            foreach ($emailArrUserTab as $emailArrUserTabVal) {
                                $emailArrUserTabDeal[] = $emailArrUserTabVal['email'];
                                $emailArrUserTabDealArr[$emailArrUserTabVal['email']] = $emailArrUserTabVal;
                            }
                            foreach ($userInfoArrDeal as $userInfoArrDealVal) {
                                if (!in_array($userInfoArrDealVal['email'],$emailArrUserTabDeal)) { // 说明是新的数据要插入
                                    $userInsert[] = $userInfoArrDealVal;
                                } elseif($userInfoArrDealVal['firstname'] != $emailArrUserTabDealArr[$userInfoArrDealVal['email']]['firstname']) { // 说明需要更新
                                    $userUpdate[] = $userInfoArrDealVal;
                                }
                            }
                        } else {
                            $userInsert = $userInfoArrDeal;
                        }

                        // 处理数据
                        if ($userInsert) {
                            $user->insertTagUser(31,$userInsert);
                        }

                        if ($userUpdate) {
                            $user->updateTagUser(31,$userUpdate);
                        }
                        $userTag->updateUserUnDeal($idArr);
                        
                        // 提交
                        $trans->commit();
                    } catch (\Exception $ex) {
                        echo $ex->getMessage();
                        $trans->rollBack();
                    }
                    $userInfoArrDeal = [];
                    $emailArr = [];
                    $userInsert = []; // 需要插入的数据
                    $userUpdate = []; // 需要更新的用户数据
                    $idArr=[];
                    $i = 0;
                }
            } else {
                continue;
            }
        }
        
        // 查询哪些邮箱在user表中已经存在存在则更新不存在则插入
        if ($emailArr) {
            try {
                // 查询处理数据
                $trans = Yii::$app->db->beginTransaction(); // 开启事务
                // 从user表中查询数据
                $emailArrUserTab = $user->getUserInfoByEmail(31,$emailArr);
               
                if ($emailArrUserTab) {
                    $emailArrUserTabDeal = [];
                    $emailArrUserTabDealArr = [];
                    foreach ($emailArrUserTab as $emailArrUserTabVal) {
                        $emailArrUserTabDeal[] = $emailArrUserTabVal['email'];
                        $emailArrUserTabDealArr[$emailArrUserTabVal['email']] = $emailArrUserTabVal;
                    }
                   
                    foreach ($userInfoArrDeal as $userInfoArrDealVal) {
                        if (!in_array($userInfoArrDealVal['email'],$emailArrUserTabDeal)) { // 说明是新的数据要插入
                            $userInsert[] = $userInfoArrDealVal;
                        } elseif($userInfoArrDealVal['firstname'] != $emailArrUserTabDealArr[$userInfoArrDealVal['email']]['firstname']) { // 说明需要更新
                            $userUpdate[] = $userInfoArrDealVal;
                        }
                    }
                } else {
                    $userInsert = $userInfoArrDeal;
                }
               
                // 处理数据
                if ($userInsert) {
                    $user->insertTagUser(31,$userInsert);
                }
                if ($userUpdate) {
                    $user->updateTagUser(31,$userUpdate);
                }
                $userTag->updateUserUnDeal($idArr);
                // 提交
                $trans->commit();
            } catch (\Exception $ex) {
                echo $ex->getMessage();
                $trans->rollBack();
            }  
        }
    }

    /**
     * yumao
     * 2015-5-22
     * 把第三方分群的相关数据加入到user表中
     */
    public function actionLangriaDealTagUser()
    {
        ignore_user_abort(true);
        set_time_limit(0);
        // 从user_ebay_tag表中取出指定数量的数据
        $oneCount = 20000;
        $userTag = new UserTag(25);
        $user = new User(25);
        $userInfoArr = $userTag->getUserUnDeal($oneCount); // 查出指定数量数据
        $userInfoArrDeal = [];
        $emailArr = [];
        $userInsert = []; // 需要插入的数据
        $userUpdate = []; // 需要更新的用户数据
        $idArr =[];
        // 处理数据去重
        $oneTimeDeal = 1000; // 一次处理1000条数据
        $i=0;
        $time = time();
        foreach ($userInfoArr as $userInfoArrOne) {
            $userInfoArrOne['update_time'] = $time;
            $userInfoArrOne['import_type'] = 1;    //手动导入
            $idArr[] = $userInfoArrOne['id'];
            if (!in_array($userInfoArrOne['email'],$emailArr)) {
                $userInfoArrDeal[$userInfoArrOne['email']] = $userInfoArrOne;
                $emailArr[] = $userInfoArrOne['email'];
                $i++;
                if ($i >= $oneTimeDeal) { // 集满指定的数据
                    try {
                        // 查询处理数据
                        $trans = Yii::$app->db->beginTransaction(); // 开启事务
                        // 从user表中查询数据
                        $emailArrUserTab = $user->getUserInfoByEmail(25,$emailArr);
                        if ($emailArrUserTab) {
                            $emailArrUserTabDeal = [];
                            $emailArrUserTabDealArr = [];
                            foreach ($emailArrUserTab as $emailArrUserTabVal) {
                                $emailArrUserTabDeal[] = $emailArrUserTabVal['email'];
                                $emailArrUserTabDealArr[$emailArrUserTabVal['email']] = $emailArrUserTabVal;
                            }
                            foreach ($userInfoArrDeal as $userInfoArrDealVal) {
                                if (!in_array($userInfoArrDealVal['email'],$emailArrUserTabDeal)) { // 说明是新的数据要插入
                                    $userInfoArrDealVal['update_time'] = $time;
                                    $userInfoArrDealVal['import_type'] = 1;    //手动导入
                                    $userInsert[] = $userInfoArrDealVal;
                                } elseif($userInfoArrDealVal['firstname'] != $emailArrUserTabDealArr[$userInfoArrDealVal['email']]['firstname']) { // 说明需要更新
//                                    $userInfoArrDealVal['update_time'] = $time;
//                                    $userInfoArrDealVal['import_type'] = 1;    //手动导入
//                                    $userUpdate[] = $userInfoArrDealVal;
                                }
                            }
                        } else {
                            $userInsert = $userInfoArrDeal;
                        }

                        // 处理数据
                        if ($userInsert) {
                            $user->insertLangriaTagUser(25,$userInsert);
                        }

                        if ($userUpdate) {
                            $user->updateTagUser(25,$userUpdate);
                        }
                        $userTag->updateUserUnDeal($idArr);

                        // 提交
                        $trans->commit();
                    } catch (\Exception $ex) {
                        echo $ex->getMessage();
                        $trans->rollBack();
                    }
                    $userInfoArrDeal = [];
                    $emailArr = [];
                    $userInsert = []; // 需要插入的数据
                    $userUpdate = []; // 需要更新的用户数据
                    $idArr=[];
                    $i = 0;
                }
            } else {
                continue;
            }
        }

        // 查询哪些邮箱在user表中已经存在存在则更新不存在则插入
        if ($emailArr) {
            try {
                // 查询处理数据
                $trans = Yii::$app->db->beginTransaction(); // 开启事务
                // 从user表中查询数据
                $emailArrUserTab = $user->getUserInfoByEmail(25,$emailArr);

                if ($emailArrUserTab) {
                    $emailArrUserTabDeal = [];
                    $emailArrUserTabDealArr = [];
                    foreach ($emailArrUserTab as $emailArrUserTabVal) {
                        $emailArrUserTabDeal[] = $emailArrUserTabVal['email'];
                        $emailArrUserTabDealArr[$emailArrUserTabVal['email']] = $emailArrUserTabVal;
                    }

                    foreach ($userInfoArrDeal as $userInfoArrDealVal) {
                        if (!in_array($userInfoArrDealVal['email'],$emailArrUserTabDeal)) { // 说明是新的数据要插入
                            $userInfoArrDealVal['update_time'] = $time;
                            $userInfoArrDealVal['import_type'] = 1;    //手动导入
                            $userInsert[] = $userInfoArrDealVal;
                        } elseif($userInfoArrDealVal['firstname'] != $emailArrUserTabDealArr[$userInfoArrDealVal['email']]['firstname']) { // 说明需要更新
//                            $userInfoArrDealVal['update_time'] = $time;
//                            $userInfoArrDealVal['import_type'] = 1;    //手动导入
//                            $userUpdate[] = $userInfoArrDealVal;
                        }
                    }
                } else {
                    $userInsert = $userInfoArrDeal;
                }

                // 处理数据
                if ($userInsert) {
                    $user->insertLangriaTagUser(25,$userInsert);
                }
                if ($userUpdate) {
                    $user->updateTagUser(25,$userUpdate);
                }
                $userTag->updateUserUnDeal($idArr);
                // 提交
                $trans->commit();
            } catch (\Exception $ex) {
                echo $ex->getMessage();
                $trans->rollBack();
            }
        }
    }
}